<?php
/**
 *
 *
 * @author	Chashin Evgeniy
 * @version 1.0.0
 */
class User_AclController extends Zen_Controller_Admin {


	public function indexAction(){
		if($this->_hasParam('role')){
			$id=preg_replace('/\D/', '', $this->_getParam('role'));
			$roletree=new User_Model_Role();
			$role=$roletree->select()->setColumns(array('tree'=>array('id'),'role'=>array('label','resources')))->getOne("`tree`.`id`=".$id);

			$this->view->role=$role;

			if($role['resources']!=''){
				$res=unserialize($role['resources']);
			}else{
				$res=array();
			}
			$this->view->res=$res;


			$this->view->resources=Zen_Acl::loadRes();
		}
	}

	public function addAction(){
		$id=$this->_getParam('id');
		$role=preg_replace('/\D/', '', $this->_getParam('roleid'));
		$roletree=new User_Model_Role();
		$role=$roletree->select()->setColumns(array('tree'=>array('id'),'role'=>array('label','resources')))->getOne("`tree`.`id`='".$role."'");
		if($role['resources']!=''){
			$res=unserialize($role['resources']);
		}else{
			$res=array();
		}

		if(!isset($res[$id])){
			$res[$id]=$id;
		}

		$roletree->update($role, array('resources'=>serialize($res)));
		$this->_helper->json(array('success'=>true));
	}

	public function removeAction(){
		$id=$this->_getParam('id');
		$role=preg_replace('/\D/', '', $this->_getParam('roleid'));
		$roletree=new User_Model_Role();
		$role=$roletree->select()->setColumns(array('tree'=>array('id'),'role'=>array('label','resources')))->getOne("`tree`.`id`='".$role."'");
		if($role['resources']!=''){
			$res=unserialize($role['resources']);
		}else{
			$res=array();
		}

		if(isset($res[$id])){
			unset($res[$id]);
		}

		$roletree->update($role, array('resources'=>serialize($res)));
		$this->_helper->json(array('success'=>true));
	}

}
